package com.example.administrator.baseproject;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import baseinterface.NetChangeDeal;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.ActivityUtil;
import utils.LoadingUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseFragment extends RootFragment {
    public FragmentActivity mContext;
    public boolean isVisiable;
    public static final String TAG = "BaseFragment";
    public View loadingView, emptyView;
    public View view;
    public SharedPreferences sharedPreferences;
    public MyHandler allHandler = SingletonClass.getSingleton(MyHandler.class);
    public BaseFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext =  getActivity();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        sharedPreferences = ActivityUtil.getSharedPreferences(mContext);
        initView();
        setListener();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisiable = true;
            onVisiable();
        } else {
            isVisiable = false;
            onUnVisiable();
        }
    }

    protected void initView() {
    }

    protected void setListener() {
    }

    public void showLoadingView(ViewGroup viewGroup) {
        if (loadingView == null) {
            loadingView = LoadingUtils.showLoadingView(viewGroup,mContext, "正在加载");
        }
    }

    protected void showLoadingView(ViewGroup viewGroup,String msg) {
        if (loadingView == null) {
            loadingView = LoadingUtils.showLoadingView(viewGroup,mContext, msg);
        }
    }

    protected void hideLoadingView() {
        LoadingUtils.hideLoadingView(loadingView);
        loadingView = null;
    }

    protected void showEmptyView(ViewGroup viewGroup) {
        if (emptyView == null) {
            emptyView = LoadingUtils.showEmptyView(viewGroup, mContext, "暂无数据", R.mipmap.nomub);
        }
    }

    protected void showEmptyView(ViewGroup viewGroup, String msg, int imgRes) {
        if (emptyView == null) {
            emptyView = LoadingUtils.showEmptyView(viewGroup, mContext, msg, imgRes);
        }
    }

    protected void hideEmptyView() {
        LoadingUtils.hideEmptyView(emptyView);
        emptyView = null;
    }

    public void onVisiable(){}
    public void onUnVisiable(){}

}
